package uires
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import mx.graphics.codec.JPEGEncoder;
	import mx.graphics.codec.PNGEncoder;
	
	import spark.components.Label;
	
	
	public class UI_Res
	{
		private static var _ins:UI_Res;
		
		public static var exportUrl:String;
		public static var importRoot:String;
		public static var printMsg:Label;
		
		public static var dataDic:Object = {};
		
		private var _cfgStream:FileStream;
		private var _urlLoader:URLLoader;
		
		public var resType:int = 2;
		private var _path:String;
		private var _dataPath:String;
		private var _rootPath:String;
		private var _outPathDic:Object = {};
		private var _pngArr:Array = [];//已压缩好的PNG数组
		
		private var _subDirec:String;
		
		private var _timer:Timer;
		private var _timer2:Timer;
		private var _ani:Animator;
		private var _loadedCount:int = 0;
		
		private var _loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
		
		private static var _bmdMap:Dictionary = new Dictionary();
		
		private var _panelName:String;
		private var _outPath:String;
		private var _outCount:int = 1;
		
		
		private static var _imgIndexDic:Dictionary = new Dictionary();
		private static function getImgIndex(panelName:String):int
		{
			if(_imgIndexDic[panelName] == null){
				_imgIndexDic[panelName] = 0;
			}
			_imgIndexDic[panelName] ++;
			return _imgIndexDic[panelName];
		}
		
		public static function get ins():UI_Res
		{
			if(!_ins){
				_ins = new UI_Res();
			}
			return _ins;
		}
		
		public function UI_Res()
		{
		}
		public function doLoad(panelName:String, path:String):void
		{
			//"C:\Users\Administrator\Desktop\ani原始\21000\bg_tiles\1.mp"//路径格式是这样的而已
			
			_panelName = panelName;
			_path = path;
			
			loadData();
		}
		/**
		 * 如果有地图数据，则加载
		 */
		private function loadData():void
		{
			var suffix:String = ".png";
			if(_path.indexOf("jpg.") == 0 || _path.lastIndexOf(".jpg") >= _path.length-4){
				suffix = ".jpg";
				resType = 1;
			}
//			_outPath = exportUrl + "\\" + _panelName + "\\" + getImgIndex(_panelName) + suffix;
			_outPath = _panelName + "/" + getImgIndex(_panelName) + suffix;
			
			if(_path.indexOf("png.") == 0 || _path.indexOf("jpg.") == 0){
				//先加载swf
				var endIndex:int = _path.indexOf(".", 4);
				var swfname:String = _path.substring(4, endIndex);
				var swfloader:swf_loader = new swf_loader();
				swfloader.startLoad("http://192.168.1.200/mhj2/client_main/ui/" + swfname + ".swf", swfComplete);
			}
			else{
				var _loader:Loader = new Loader();
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgComplete);
				_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onImgError);
				_loader.load(new URLRequest(_path), _loaderContext);
			}
		}
		private function swfComplete(swf:DisplayObject):void
		{
			var bmd:BitmapData = getBaseBitmapData(_path);
			
			writeImg(bmd, _outPath);
		}
		private function onImgComplete(e:Event):void
		{
			var info:LoaderInfo = e.currentTarget as LoaderInfo;
			var bmd:BitmapData = (info.content as Bitmap).bitmapData;
			
			writeImg(bmd, _outPath);
		}
		private function onImgError(e:IOErrorEvent):void
		{
			trace("报错：" + _path);
		}
		/**获取类*/
		public function getClass(name:String):Class {
			
			if(hasClass(name)) {
				return _domain.getDefinition(name) as Class;
			}
			return null;
		}
		/**判断是否有类的定义*/
		public function hasClass(name:String):Boolean {
			
			var b:Boolean = _domain.hasDefinition(name);
			if(!b){
//				if(name.indexOf("png.") == 0 || name.indexOf("jpg.") == 0){
//					mytrace("【UI资源不存在】：" + name);
//				}
			}
			return b;
		}
		/**获取位图数据*/
		public function getBaseBitmapData(name:String, cache:Boolean = true):BitmapData {
			
			var bmd:BitmapData = _bmdMap[name];
			if(bmd == null) {
				var bmdClass:Class = getClass(name);
				if(bmdClass == null) {
					trace("【UI资源获取失败】：" + name);
					return null;
				}
				bmd = new bmdClass(1, 1);
				if(cache) {
					_bmdMap[name] = bmd;
				}
			}
			return bmd;
		}
		
		private function get _domain():ApplicationDomain
		{
			return ApplicationDomain.currentDomain;
		}
		
		
		//------------output=------------------------
		public function writeImg(param1:BitmapData, path:String):void
		{
			//			var path:String = _path.replace(importRoot, "");
			//			if(path.indexOf("\\") == 0){
			//				path = path.substring(1);
			//			}
			
			var imgByteArray:ByteArray;
			var jpgenc:JPEGEncoder;
			var pngenc:PNGEncoder;
			var msg:Label;
			var bmd:BitmapData = param1;
			var laststr:String;
			if(resType == 1)
			{
				jpgenc = new JPEGEncoder(80);
				imgByteArray = jpgenc.encode(bmd);
				path = path.replace(".mp", ".jpg");
			}
			else
			{
				pngenc = new PNGEncoder();
				imgByteArray = pngenc.encode(bmd);
				path = path.replace(".mp", ".png");
			}
			_outPathDic[path] = false;
			
			var rootFile:File = new File(exportUrl);
			var fl:File = rootFile.resolvePath(path);
			var fs:FileStream = new FileStream();
			
			try
			{
				fs.open(fl, FileMode.WRITE);
				fs.writeBytes(imgByteArray);
				fs.close();
				
			}
			catch(e:Error)
			{
				throw new Error("打包出错：" + path);
			}
		}
		
		
	}
}
import flash.display.BitmapData;

class Animator
{
	
	private var _totalMovieTime:int = 1;
	private var _isDispose:Boolean = false;
	private var _blendMode:String = "normal";
	private var _url:String;
	private var _currentFrame:int = -1;
	private var _delay:int = 0;
	private var _list:Array;
	private var _parmas:Object;
	private var _loop:Boolean;
	private var _width:int = -1;
	private var _height:int = -1;
	
	public var YER:String = "";
	
	public function get list():Array
	{
		return _list;
	}
	
	public function Animator()
	{
		this._list = [];
	}
	public function addImageAt(image:SingleImage, index:int):void
	{
		image.animator = this;
		this._list.splice(index, 0, image);
	}
	public function replaceImageAt(image:SingleImage, index:int):void
	{
		image.animator = this;
		this._list[index] = image;
	}
	public function pushImage(image:SingleImage):uint
	{
		image.animator = this;
		this._totalMovieTime = (this._totalMovieTime + image.delayTime);
		return (this._list.push(image));
	}
	public function getImage(index:uint):SingleImage
	{
		if (this._list)
		{
			return (this._list[index]);
		};
		return (null);
	}
	public function get width():int
	{
		var image:* = null;
		var w:int;
		if (this._width == -1)
		{
			for each (image in this._list)
			{
				w = (image.width + image.offsetX);
				if (this._width < w)
				{
					this._width = w;
				};
			};
		};
		return (this._width);
	}
	public function get height():int
	{
		var image:* = null;
		var h:int;
		if (this._height == -1)
		{
			for each (image in this._list)
			{
				h = (image.height + image.offsetY);
				if (this._height < h)
				{
					this._height = h;
				};
			};
		};
		return (this._height);
	}
	public function get totalMovieTime():int
	{
		return (this._totalMovieTime);
	}
	public function get length():uint
	{
		return (this._list.length);
	}
	public function get renderDelay():int
	{
		return (this._delay);
	}
	public function set renderDelay(value:int):void
	{
		this._delay = value;
	}
	public function get url():String
	{
		return (this._url);
	}
	public function set url(value:String):void
	{
		this._url = value;
	}
	public function get parmas():Object
	{
		return (this._parmas);
	}
	public function set parmas(value:Object):void
	{
		this._parmas = value;
	}
	public function get loop():Boolean
	{
		return (this._loop);
	}
	public function set loop(value:Boolean):void
	{
		this._loop = value;
	}
	public function get blendMode():String
	{
		return (this._blendMode);
	}
	public function set blendMode(value:String):void
	{
		this._blendMode = value;
	}
	public function get isDispose():Boolean
	{
		return (this._isDispose);
	}
	public function dispose():void
	{
		var image:* = null;
		var i:int;
		var leng:int = this._list.length;
		while (i < leng)
		{
			image = this._list[i];
			image.dispose();
			i++;
		};
		this._list = null;
		this._isDispose = true;
	}
	
}



class SingleImage
{
	
	public var bitmapData:BitmapData;
	public var width:Number = 0;
	public var height:Number = 0;
	public var offsetX:Number = 0;
	public var offsetY:Number = 0;
	public var offsetX2:Number = 0;
	public var offsetY2:Number = 0;
	public var frameNum:int;
	public var imageName:String;
	public var delayTime:int = 0;
	public var animator:Animator;
	protected var _isDispose:Boolean = false;
	
	public function SingleImage(bitmapdata:BitmapData=null)
	{
		this.bitmapData = bitmapdata;
		if (this.bitmapData)
		{
			this.width = this.bitmapData.width;
			this.height = this.bitmapData.height;
		};
	}
	public function get isDispose():Boolean
	{
		return (this._isDispose);
	}
	public function dispose():void
	{
		if (this.bitmapData)
		{
			this.bitmapData.dispose();
		};
		this.bitmapData = null;
		this.width = 0;
		this.height = 0;
		this.offsetX = 0;
		this.offsetY = 0;
		this.frameNum = 0;
		this.imageName = "";
		this.animator = null;
		this._isDispose = true;
	}
	
}